<template>
    <div class="goods-list">
        <!-- <router-link class="goods-item" v-for="item in goodslist" :key="item.id" :to="'/home/goodsinfo/' + item.id" tag="div">
            <img :src="item.img_url" alt="">
            <h1 class="title">{{ item.title }}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{ item.sell_price }}</span>
                    <span class="old">￥{{ item.market_price }}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩{{ item.stock_quantity }}件</span>
                </p>
            </div>
        </router-link> -->
        <!-- 网页中的两种跳转方式：1.使用 a 标签的方式；2.使用 window.location.href 的形式 -->
        <div class="goods-item" v-for="item in goodslist" :key="item.id" @click="goDetail(item.id)">
            <img :src="item.img_url" alt="">
            <h1 class="title">{{ item.title }}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{ item.sell_price }}</span>
                    <span class="old">￥{{ item.market_price }}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩{{ item.stock_quantity }}件</span>
                </p>
            </div>
        </div>
        <!-- <mt-button type="danger" size="large" icon="more" @click="getMore">加载更多</mt-button> -->
        <mt-button type="danger" size="large" icon="more">加载更多</mt-button>
    </div>
</template>

<script>
export default {
    data(){
        return {
            pageindex: 1,  //分页的页数
            goodslist: [
                { id: 1, img_url: "https://img.alicdn.com/bao/uploaded/O1CN01D0QRqi2JGV9ov5xeK_!!0-item_pic.jpg_240x240.jpg", title: "手持熨烫机家用小型电熨斗蒸汽刷便携挂烫机出行必备", sell_price: "199.90", market_price: "250.00", stock_quantity: "970"},
                { id: 2, img_url: "https://img.alicdn.com/bao/uploaded/O1CN01CjPxm42JGV9t14GyK_!!0-item_pic.jpg_240x240.jpg", title: "心选高硼硅玻璃凉水壶冷水壶套装耐高温泡茶壶套装", sell_price: "59.90", market_price: "80.00", stock_quantity: "4115"},
                { id: 3, img_url: "https://img.alicdn.com/bao/uploaded/O1CN01beSWIs2JGV9mYtwOb_!!0-item_pic.jpg_240x240.jpg", title: "心选x画法几何M8大肚几何系列装饰花瓶陶瓷简约风格花瓶", sell_price: "49.90", market_price: "80.00", stock_quantity: "271"},
                { id: 4, img_url: "https://img.alicdn.com/bao/uploaded/O1CN01E6hR5V2JGVA3hYW3t_!!0-item_pic.jpg_240x240.jpg", title: "心选分盒收纳盒药盒首饰分装盒", sell_price: "12.90", market_price: "50.00", stock_quantity: "829"},
                { id: 5, img_url: "https://img.alicdn.com/bao/uploaded/O1CN01Tgva6t2JGVCpKKdN8_!!3294889394.jpg_240x240.jpg", title: "心选蒜香青豆120g坚果炒货豌豆休闲零食独立小包", sell_price: "9.90", market_price: "19.00", stock_quantity: "1328"},
                { id: 6, img_url: "https://img.alicdn.com/bao/uploaded/O1CN01V737Tp2JGVCnoiikK_!!3294889394.jpg_240x240.jpg", title: "FollowMe城市香氛系列泡沫洗手液青色岚山留香易冲洗250ml", sell_price: "19.80", market_price: "20.00", stock_quantity: "20"},
                { id: 7, img_url: "https://img.alicdn.com/bao/uploaded/O1CN01G6A9g52JGV8tFCo16_!!0-item_pic.jpg_240x240.jpg", title: "心选彩妆三角塑形自然眉笔眉粉刷初学者不易脱色防水防汗", sell_price: "29.80", market_price: "35.00", stock_quantity: "3918"},
                { id: 8, img_url: "https://img.alicdn.com/bao/uploaded/O1CN01w4O2Fa2JGVABb7DU1_!!0-item_pic.jpg_240x240.jpg", title: "心选颈肩按摩仪颈椎按摩器颈部腰部肩部家用揉捏肩颈脖护颈仪", sell_price: "359.90", market_price: "400.00", stock_quantity: "329"},
            ]
            // goodslist: []  //存放服务器商品列表的数组
        };
    },
    // created(){
    //     this.getGoodsList();
    // },
    methods:{
        // getGoodsList(){  //获取服务器商品列表数据
        //     this.$http.get('api/getgoods?pageindex=' + this.pageindex).then(result => {
        //         if(result.body.status === 0){
        //             //this.goodslist = result.body.message;
        //             this.goodslist = this.goodslist.concat(result.body.message);
        //         }
        //     });
        // },
        getMore(){
            this.pageindex++;
            this.getGoodsList();
        },
        goDetail(id){  //使用JS形式进行路由导航
            // 注：区分 this.$route 和 this.$router
            //     其中：this.$route是路由参数对象，如params、query；this.$router是路由导航对象，用JS代码实现路由的前进后退跳转到新URL
            // this.$router.push('/home/goodsinfo/' + id);   //方式1
            // this.$router.push({ path: '/home/goodsinfo/' + id});   //方式2传递对象
            this.$router.push({ name: 'goodsinfo', params: { id }});  //方式3传递命名的路由
        }
    }
}
</script>

<style lang="scss" scoped>
.goods-list{
    display: flex;
    flex-wrap: wrap;
    padding: 7px;
    justify-content: space-between;
    .goods-item{
        width: 49%;
        border: 1px solid #ccc;
        box-shadow: 0 0 8px #ccc;
        margin: 4px 0;
        padding: 2px;
        display: flex;
        flex-direction: column;   //改变主轴方向
        justify-content: space-between;
        min-height: 293px;
        img{
            width: 100%;
        }
        .title{
            font-size: 14px;
        }
        .info{
            background-color: #eee;
            p{
                margin: 0;
                padding: 5px;
            }
            .price{
                .now{
                    color: red;
                    font-weight: bold;
                    font-size: 16px;
                }
                .old{
                    text-decoration: line-through;
                    font-size: 12px;
                    margin-left: 10px;
                }
            }
            .sell{
                display: flex;
                justify-content: space-between;
                font-size: 13px;
            }
        }
    }
}
</style>